<!--
 * @Author: hq
 * @Date: 2022-05-28 19:29:52
 * @LastEditors: hq
 * @LastEditTime: 2022-07-07 10:34:14
 * @Description: 添加分类弹窗
 * @version: 1.0
-->
<template>
  <div class="change">
    <el-row>
      <el-col :span="16">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="昵称" prop="nickName">
            <el-input
              clearable
              v-model="ruleForm.nickName"
              placeholder="请输入昵称"
            ></el-input>
          </el-form-item>

          <el-form-item label="密码">
            <el-input
              clearable
              maxlength="10"
              show-password
              v-model="ruleForm.password"
              placeholder="如需修改密码，可填写新密码"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input
              v-model="ruleForm.phone"
              placeholder="请输入分类名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱" v-if="this.rowData.id">
            <el-input
              clearable
              v-model="ruleForm.email"
              placeholder="请输入邮箱"
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱" v-else>
            <el-input
              v-model="ruleForm.email"
              placeholder="请输入邮箱"
            ></el-input>
          </el-form-item>
          <el-form-item label="头像" :required="true">
            <el-upload
              v-loading="loading1"
              class="avatar-uploader"
              action=""
              :show-file-list="false"
              :http-request="upLoadServe"
            >
              <img
                v-if="ruleForm.imageUrl"
                :src="ruleForm.imageUrl"
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="性别" prop="sex" :required="true">
            <el-radio-group v-model="ruleForm.sex">
              <el-radio label="1">男</el-radio>
              <el-radio label="2">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="生日" prop="birthday">
            <el-date-picker
              value-format="yyyy-MM-dd"
              v-model="ruleForm.birthday"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="邀请码" prop="code">
            <el-input
              disabled
              v-model="ruleForm.code"
              placeholder="请输入邀请码"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >确定</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { upLoad } from "@/api/index";
import { add_edit_user } from "@/api/member";
import upImg from "@/utils/upimg";
export default {
  props: ["rowData"],
  data() {
    return {
      loading1: false,
      rowId: "",
      ruleForm: {
        nickName: "",
        password: "",
        phone: "",
        email: "",
        imageUrl: "",
        imageUrlId: "",
        sex: "1",
        birthday: "",
        code: "",
      },
      rules: {
        nickName: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        code: [{ required: true, message: "请输入邀请码", trigger: "blur" }],
        // birthday: [
        //   {
        //     type: "date",
        //     required: true,
        //     message: "请选择日期",
        //     trigger: "change",
        //   },
        // ],
      },
      upClassifyArr: [{ id: 0, name: "顶级菜单" }],
    };
  },
  methods: {
    //提交
    async add_edit_user() {
      const { err, msg } = await add_edit_user({
        id: this.rowData.id,
        pid: this.ruleForm.code,
        nickname: this.ruleForm.nickName,
        email: this.ruleForm.email,
        phone: this.ruleForm.phone,
        avatar: this.ruleForm.imageUrlId,
        sex: this.ruleForm.sex,
        birthday: this.ruleForm.birthday,
        password: this.ruleForm.password,
      });
      if (err === 0) {
        this.$emit("closeDialog");
        this.$message({
          showClose: true,
          message: "操作成功",
          type: "success",
        });
      } else {
        this.$message({
          showClose: true,
          message: msg,
          type: "error",
        });
      }
    },
    //上传图片
    async upLoad(formData) {
      const { data, err } = await upLoad(formData);
      if (err === 0) {
        this.ruleForm.imageUrl = data.file_src;
        this.ruleForm.imageUrlId = data.file_id;
      }
    },
    upLoadServe(file) {
      this.loading1 = true;
      upImg.upimg(
        (data) => {
          //state判断是首图还是轮播图
          this.ruleForm.imageUrl = data.data.pre_img;
          this.ruleForm.imageUrlId = data.data.url;
          this.loading1 = false;
        },
        1,
        file
      );
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          //接口()
          this.add_edit_user();
        }
      });
    },
  },
  created() {
    //    nickName: "",
    //     password: "",
    //     phone: "",
    //     email: "",
    //     imageUrl: "",
    //     imageUrlId: "",
    //     sex: "1",
    //     birthday: "",
    //     code: "",
    if (this.rowData.id) {
      //编辑
      console.log(this.rowData);
      this.ruleForm.nickName = this.rowData.nickname;
      this.ruleForm.phone = this.rowData.phone;
      this.ruleForm.email = this.rowData.email;
      this.ruleForm.imageUrl = this.rowData.avatar.src;
      this.ruleForm.imageUrlId = this.rowData.avatar.at_id;
      this.ruleForm.sex = this.rowData.sex.toString();
      this.ruleForm.birthday = this.rowData.birthday;
      this.ruleForm.code = this.rowData.pid;
    }
  },
};
</script>

<style lang="scss" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>
